#!/usr/bin/env bash

module_name=$1
echo "\033[31m cur tencent cloud product is:$1 \033[0m"
echo "\033[31m start get cur product:$1 lib and include files from cos \033[0m"
# lib path : /usr/local/lib/
# include files path : /usr/local/include/
wget -P /usr/local/lib/  https://tencentcloud-sdk-1305431714.cos.ap-shanghai.myqcloud.com/cpp/lib/libtencentcloud-sdk-cpp-core.so
wget -P /usr/local/lib/  https://tencentcloud-sdk-1305431714.cos.ap-shanghai.myqcloud.com/cpp/lib/libtencentcloud-sdk-cpp-$module_name.so
wget https://tencentcloud-sdk-1305431714.cos.ap-shanghai.myqcloud.com/cpp/include/tencentcloud.tgz
tar -zxvf tencentcloud.tgz -C /usr/local/include/
echo "\033[31m end get cur product:$1 lib and include files from cos \033[0m"
#Generate the CMakeList corresponding to the tencent_cloud_cpp_sample
basepath=$(cd `dirname $0`; pwd)
echo "\033[31m tencent_cloud_cpp_sample file path is: $basepath \033[0m"

cd tencent_cloud_cpp_sample
rm -f ./CMakeLists.txt
echo "cmake_minimum_required (VERSION 3.10)" >> ./CMakeLists.txt
echo "project(tencent_cloud_cpp_sample)" >> ./CMakeLists.txt
echo "set(CMAKE_CXX_STANDARD 11)" >> ./CMakeLists.txt
echo "add_executable(tencent_cloud_cpp_sample tencent_cloud_cpp_sample.cpp)" >> ./CMakeLists.txt
echo "target_link_libraries(tencent_cloud_cpp_sample tencentcloud-sdk-cpp-$module_name tencentcloud-sdk-cpp-core)" >> ./CMakeLists.txt

#modify cpp Credential interface add cloud_secret_id cloud_secret_key cloud_token param
sed -i 's/Credential cred = Credential("SecretId", "SecretKey")/std::string cloud_secret_id = getenv("cloud_secret_id"); \n  \
        std::string cloud_secret_key = getenv("cloud_secret_key"); \n \
        std::string cloud_token = getenv("cloud_token"); \n \
        Credential cred = Credential(cloud_secret_id, cloud_secret_key, cloud_token)/g' tencent_cloud_cpp_sample.cpp

#complie tencent_cloud_cpp_sample
rm -rf build/
echo "\033[31m create tencent_cloud_cpp_sample compile dir：build \033[0m"
mkdir build
echo "\033[31m into tencent_cloud_cpp_sample compile dir：build \033[0m"
cd build/
echo "\033[31m do tencent_cloud_cpp_sample:cmake \033[0m"
cmake ../
echo "\033[31m do tencent_cloud_cpp_sample:make \033[0m"
make

basepath=$(cd `dirname $0`; pwd)
echo "\033[31m tencent_cloud_cpp_sample compiled programe file path is: $basepath \033[0m"
